package com.duobaoyu.dby.supply.api.service.impl;

import com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderStatusEnum;
import com.duobaoyu.dby.channel.boundary.bean.order.param.LogisticQueryParam;
import com.duobaoyu.dby.supply.api.service.LogisticsBizService;
import com.duobaoyu.dby.supply.biz.client.boundary.order.OrderBoundaryClient;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderDeliveryBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderInfoBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderQueryBO;
import com.duobaoyu.dby.supply.biz.service.order.OrderService;
import com.duobaoyu.dby.supply.common.biz.remote.dto.order.OrderDeliveryDTO;
import com.duobaoyu.dby.supply.common.biz.remote.to.order.DeliveryTO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 物流业务实现
 *
 * @Author yangyong
 * @Date 2023/9/27
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LogisticsBizServiceImpl implements LogisticsBizService{
	private final OrderBoundaryClient orderBoundaryClient;
	private final OrderService orderService;

	@Override
	public List<DeliveryTO> getOrderDelivery(OrderDeliveryDTO orderDelivery) {
		OrderQueryBO orderQuery = new OrderQueryBO();
		orderQuery.setOrderSnList(orderDelivery.getOrderSnList());
		List<OrderInfoBO> orderInfoList = orderService.listOrderInfo(orderQuery);
		CustomAssert.notEmpty(orderInfoList,"订单不存在");

		List<OrderInfoBO> shippedOrderList = orderInfoList.stream().filter(item -> OrderStatusEnum.checkLogistics(item.getOrderStatus())).collect(Collectors.toList());
		if(CollectionUtils.isEmpty(shippedOrderList)){
			return new ArrayList<>();
		}

		List<String> orderSnList = shippedOrderList.stream().map(OrderInfoBO::getOrderSn).collect(Collectors.toList());
		List<LogisticQueryParam.ExpressInfo> expressInfo = orderService.getExpressInfo(orderSnList);
		List<OrderDeliveryBO> orderDeliveryList = orderBoundaryClient.queryLogisticInfoList(orderDelivery.getOrderSnList(), shippedOrderList,expressInfo);
		return orderDeliveryList.stream().map(delivery -> {
			List<DeliveryTO.DeliveryInfo> deliveryInfoList = delivery.getDeliveryInfoList().stream().map(info -> {
				List<DeliveryTO.DeliveryInfo.TrackInfo> trackInfoToList = info.getTrackInfoList().stream().map(s -> DeliveryTO.DeliveryInfo.TrackInfo.builder()
						.trackContent(s.getTrackContent())
						.trackOperator(s.getTrackOperator())
						.trackMsgTime(s.getTrackMsgTime())
						.build()).collect(Collectors.toList());
				return DeliveryTO.DeliveryInfo.builder()
						.deliveryOrderSn(info.getDeliveryOrderSn())
						.deliveryCarrier(info.getDeliveryCarrier())
						.trackInfoList(trackInfoToList)
						.build();
			}).collect(Collectors.toList());
			return DeliveryTO.builder()
					.orderSn(delivery.getOrderSn())
					.deliveryInfoList(deliveryInfoList)
					.build();
		}).collect(Collectors.toList());
	}
}
